/**
 * 新增学生信息
 */
function addStudent() {
    var data = $("#studentForm").serialize();
    // 弹出确认提示框
    var result = confirm("确定保存学生信息吗？");
    if(result) {
        $.ajax({
            url: "/webapi/student/insert",
            method: "POST",
            data: data,
            success: function () {
                loadTable();
                $('#myModal').modal('hide'); // 关闭模态框
            }
        });
    }
}
/**
 * 更新学生信息
 */
function updateStudent() {
    var data = $("#studentForm").serialize();
    var id = $("#id").val(); // 获取学生的ID
    data += "&id=" + id;
    // 弹出确认提示框
    var result = confirm("确定保存学生信息吗？");
    if(result){
        $.ajax({
            url: "/webapi/student/update", // 使用学生ID来构建更新请求的URL
            method: "PUT",
            data: data
        }).done(function () {
            loadTable();
            $('#myModal').modal('hide'); // 关闭模态框
        });
    }
    else{
    }
}
/**
 * 更新学生信息的事先查询
 * @param id
 */
function editStudent(id){
    $('#myModal').modal('show');    //打开模态框
    $('#editId').show(); // 显示ID字段
    $.ajax({
        url: "/webapi/student/get/" + id
    }).done(function (rs){
        $('#id').val(rs.id);
        $('#no').val(rs.no);
        $('#name').val(rs.name);
        $('#sex').val(rs.sex);
        $('#cno').val(rs.cno);
        $('#score').val(rs.score);
        loadTable();
    });
    // 将保存按钮的点击事件改为updateStudent()
    $('#saveButton').unbind().click(function() {
        updateStudent();
    });
}
/**
 * 删除学生信息
 * @param id 学生id
 */
function deleteStudent(id) {
    // 显示确认对话框
    var result = confirm("您确定要删除该学生信息吗？");
    // 如果用户确认删除，则进行 AJAX 请求
    if (result) {
        $.ajax({
            url: "/webapi/student/delete/" + id, // 假设这是删除学生的端点
            method: "DELETE"
        }).done(function () {
            // 删除后重新加载表格
            loadTable();
        });
    }
}
/**
 * 查询学生信息
 */
function searchStudent() {
    var searchBy = $("#searchBy").val(); // 获取查询方式
    var keyword = $("#searchInput").val().trim(); // 获取查询关键词
    if (keyword !== "") {
        $.ajax({
            url: "/webapi/student/search", // 发送查询请求的 URL
            method: "GET",
            data: {
                searchBy: searchBy,
                keyword: keyword
            }
        }).done(function (result) {
            // 处理查询结果
            if (result.length > 0) {
                // 如果查询到结果，更新表格内容
                var html = "";
                for (var i = 0; i < result.length; i++) {
                    var student = result[i];
                    html += "<tr>" +
                        "<td>" + student.id + "</td>" +
                        "<td>" + student.no + "</td>" +
                        "<td>" + student.name + "</td>" +
                        "<td>" + student.sex + "</td>" +
                        "<td>" + student.cno + "</td>" +
                        "<td>" + student.score + "</td>" +
                        "<td>" +
                        "<a href='#' onclick='editStudent(" + student.id + ");'>更新</a>" +
                        "<svg style='width: 20px; height: 20px; position: relative; top: 5px; left: 15px; cursor: pointer;' onclick='deleteStudent(" + student.id + ");' t=\"1715694995313\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1647\" width=\"200\" height=\"200\"><path d=\"M781.28 851.36a58.56 58.56 0 0 1-58.56 58.56H301.28a58.72 58.72 0 0 1-58.56-58.56V230.4h538.56z m-421.6-725.92a11.84 11.84 0 0 1 12-12h281.28a11.84 11.84 0 0 1 12 12V160H359.68zM956.8 160H734.72v-34.56a81.76 81.76 0 0 0-81.76-81.76H371.68a82.08 82.08 0 0 0-81.76 81.76V160H67.2a35.36 35.36 0 0 0 0 70.56h105.12v620.8a128.96 128.96 0 0 0 128.96 128.96h421.44a128.96 128.96 0 0 0 128.96-128.96V230.4H956.8a35.2 35.2 0 0 0 35.2-35.2 34.56 34.56 0 0 0-35.2-35.2zM512 804.16a35.2 35.2 0 0 0 35.2-35.36V393.92a35.2 35.2 0 1 0-70.4 0V768.8a35.2 35.2 0 0 0 35.2 35.36m-164.32 0a35.36 35.36 0 0 0 35.36-35.36V393.92a35.36 35.36 0 1 0-70.56 0V768.8a36.32 36.32 0 0 0 35.2 35.36m328.64 0a35.36 35.36 0 0 0 35.2-35.36V393.92a35.36 35.36 0 1 0-70.56 0V768.8a35.36 35.36 0 0 0 35.36 35.36\" fill=\"#D81E06\" p-id=\"1648\"></path></svg>" +
                        "</td>" +
                        +"</tr>";
                }
                $("#studentTb").html(html);
                $('#searchModal').modal('hide'); // 关闭模态框
            } else {
                // 如果没有查询到结果，显示提示信息
                $("#studentTb").html("<tr><td colspan='6'>未找到匹配的学生信息。</td></tr>");
                // alert("未找到匹配的学生信息，该学生不存在！");
                $('#searchModal').modal('hide'); // 关闭模态框
            }
        });
    }
}
/**
 * 查询学生选课信息
 */
function getStudentsByCourse() {
    var courseNumber = $("#courseSelect").val();
    if (courseNumber !== "") {
        $.ajax({
            url: "/webapi/student/cno/" + courseNumber, // 根据需要修改URL
            method: "GET",
        }).done(function (result) {
            if (result.length > 0) {
                var html = "";
                for (var i = 0; i < result.length; i++) {
                    var student = result[i];
                    html += "<tr>" +
                        "<td>" + student.id + "</td>" +
                        "<td>" + student.no + "</td>" +
                        "<td>" + student.name + "</td>" +
                        "<td>" + student.sex + "</td>" +
                        "<td>" + student.cno + "</td>" +
                        "<td>" + student.score + "</td>" +
                        "<td>" +
                        "<a href='#' onclick='editStudent(" + student.id + ");'>更新</a>" +
                        "<svg style='width: 20px; height: 20px; position: relative; top: 5px; left: 15px; cursor: pointer;' onclick='deleteStudent(" + student.id + ");' t=\"1715694995313\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1647\" width=\"200\" height=\"200\"><path d=\"M781.28 851.36a58.56 58.56 0 0 1-58.56 58.56H301.28a58.72 58.72 0 0 1-58.56-58.56V230.4h538.56z m-421.6-725.92a11.84 11.84 0 0 1 12-12h281.28a11.84 11.84 0 0 1 12 12V160H359.68zM956.8 160H734.72v-34.56a81.76 81.76 0 0 0-81.76-81.76H371.68a82.08 82.08 0 0 0-81.76 81.76V160H67.2a35.36 35.36 0 0 0 0 70.56h105.12v620.8a128.96 128.96 0 0 0 128.96 128.96h421.44a128.96 128.96 0 0 0 128.96-128.96V230.4H956.8a35.2 35.2 0 0 0 35.2-35.2 34.56 34.56 0 0 0-35.2-35.2zM512 804.16a35.2 35.2 0 0 0 35.2-35.36V393.92a35.2 35.2 0 1 0-70.4 0V768.8a35.2 35.2 0 0 0 35.2 35.36m-164.32 0a35.36 35.36 0 0 0 35.36-35.36V393.92a35.36 35.36 0 1 0-70.56 0V768.8a36.32 36.32 0 0 0 35.2 35.36m328.64 0a35.36 35.36 0 0 0 35.2-35.36V393.92a35.36 35.36 0 1 0-70.56 0V768.8a35.36 35.36 0 0 0 35.36 35.36\" fill=\"#D81E06\" p-id=\"1648\"></path></svg>" +
                        "</td>" +
                        "</tr>";
                }
                $("#studentTb").html(html);
                $("#page").hide();
            } else {
                $("#studentTb").html("<tr><td colspan='6'>未找到选择该课程的学生信息。</td></tr>");
            }
        });
    }
}
/**
 * 加载学生信息表格
 */var currentPage = 1;
var itemsPerPage = 6;
var sortBy = 'id'; // 默认按照 ID 排序
var sortOrder = 'asc'; // 默认升序

function loadTable() {
    $.ajax({
        url: '/webapi/student/list',
        method: 'GET',
        success: function (rs) {
            // 排序
            rs.sort((a, b) => {
                if (sortOrder === 'asc') {
                    return a[sortBy] - b[sortBy];
                } else {
                    return b[sortBy] - a[sortBy];
                }
            });

            var totalItems = rs.length;
            var totalPages = Math.ceil(totalItems / itemsPerPage);

            var startIndex = (currentPage - 1) * itemsPerPage;
            var endIndex = Math.min(startIndex + itemsPerPage, totalItems);

            var html = "";
            for (var i = startIndex; i < endIndex; i++) {
                var item = rs[i];
                html += "<tr>" +
                    "<td>" + item.id + "</td>" +
                    "<td>" + item.no + "</td>" +
                    "<td>" + item.name + "</td>" +
                    "<td>" + item.sex + "</td>" +
                    "<td>" + item.cno + "</td>" +
                    "<td>" + item.score + "</td>" +
                    "<td>" +
                    "<a href='#' onclick='editStudent(" + item.id + ");'>更新</a>" +
                    "<svg style='width: 20px; height: 20px; position: relative; top: 5px; left: 15px; cursor: pointer;' onclick='deleteStudent(" + item.id + ");' t=\"1715694995313\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1647\" width=\"200\" height=\"200\"><path d=\"M781.28 851.36a58.56 58.56 0 0 1-58.56 58.56H301.28a58.72 58.72 0 0 1-58.56-58.56V230.4h538.56z m-421.6-725.92a11.84 11.84 0 0 1 12-12h281.28a11.84 11.84 0 0 1 12 12V160H359.68zM956.8 160H734.72v-34.56a81.76 81.76 0 0 0-81.76-81.76H371.68a82.08 82.08 0 0 0-81.76 81.76V160H67.2a35.36 35.36 0 0 0 0 70.56h105.12v620.8a128.96 128.96 0 0 0 128.96 128.96h421.44a128.96 128.96 0 0 0 128.96-128.96V230.4H956.8a35.2 35.2 0 0 0 35.2-35.2 34.56 34.56 0 0 0-35.2-35.2zM512 804.16a35.2 35.2 0 0 0 35.2-35.36V393.92a35.2 35.2 0 1 0-70.4 0V768.8a35.2 35.2 0 0 0 35.2 35.36m-164.32 0a35.36 35.36 0 0 0 35.36-35.36V393.92a35.36 35.36 0 1 0-70.56 0V768.8a36.32 36.32 0 0 0 35.2 35.36m328.64 0a35.36 35.36 0 0 0 35.2-35.36V393.92a35.36 35.36 0 1 0-70.56 0V768.8a35.36 35.36 0 0 0 35.36 35.36\" fill=\"#D81E06\" p-id=\"1648\"></path></svg>" +
                    "</td>" +
                    "</tr>";
            }
            $("#studentTb").html(html);

            // 更新分页链接
            var paginationHtml = "";
            for (var page = 1; page <= totalPages; page++) {
                var activeClass = (page === currentPage) ? "active" : "";
                paginationHtml += "<li class='page-item " + activeClass + "'><a class='page-link' href='#' onclick='changePage(" + page + ")'>" + page + "</a></li>";
            }
            $("#pagination").html(paginationHtml);
        }
    });
    $("#page").show();
}

function sortTable(field) {
    // 如果当前的排序字段与点击的字段一致，则切换排序方式
    if (sortBy === field) {
        sortOrder = (sortOrder === 'asc') ? 'desc' : 'asc';
    } else {
        sortBy = field;
        sortOrder = 'asc'; // 默认升序
    }
    loadTable(); // 重新加载表格进行排序
}

function changePage(page) {
    currentPage = page;
    loadTable();
}
$(function () {
    loadTable();
    /**
     * 避免更新学生信息后点击新增学生按钮时出现之前所更新学生的信息
     */
    $('#myModal').on('show.bs.modal', function () {
        // 清空表单内容
        $('#studentForm')[0].reset();
        // 清空编号字段的值
        $('#editId').hide();
    });
});
